package com.mp.chart.renderer;

import android.graphics.Canvas;
import android.graphics.Paint;

import com.mp.chart.animation.ChartAnimator;
import com.mp.chart.buffer.SarPointBuffer;
import com.mp.chart.data.Entry;
import com.mp.chart.data.SarData;
import com.mp.chart.data.SarDataSet;
import com.mp.chart.data.SarEntry;
import com.mp.chart.interfaces.SarDataProvider;
import com.mp.chart.utils.Highlight;
import com.mp.chart.utils.Transformer;
import com.mp.chart.utils.ViewPortHandler;

import java.util.List;


public class SarChartRenderer extends DataRenderer {


    protected SarDataProvider mChart;

    private SarPointBuffer[] mPointBuffers;

    public SarChartRenderer(SarDataProvider chart, ChartAnimator animator, ViewPortHandler viewPortHandler) {
        super(animator, viewPortHandler);

        mChart = chart;
        mRenderPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    public void initBuffers() {
        SarData sarData = mChart.getSarData();
        final int dataSetCount = sarData.getDataSetCount();
        mPointBuffers = new SarPointBuffer[dataSetCount];
        for (int i = 0; i < dataSetCount; i++) {
            SarDataSet set = sarData.getDataSetByIndex(i);
            mPointBuffers[i] = new SarPointBuffer(set.getValueCount() * 2);
        }
    }

    @Override
    public void drawData(Canvas c) {
        SarData sarData = mChart.getSarData();

        for (SarDataSet set : sarData.getDataSets()) {

            if (set.isVisible())
                drawDataSet(c, set);
        }
    }

    private void drawDataSet(Canvas c, SarDataSet dataSet) {

        Transformer trans = mChart.getTransformer(dataSet.getAxisDependency());

        float phaseX = mAnimator.getPhaseX();
        float phaseY = mAnimator.getPhaseY();

        int dataSetIndex = mChart.getSarData().getIndexOfDataSet(dataSet);

        List<SarEntry> entries = dataSet.getYVals();

        Entry entryFrom = dataSet.getEntryForXIndex(mMinX);
        Entry entryTo = dataSet.getEntryForXIndex(mMaxX);

        int minx = Math.max(dataSet.getEntryPosition(entryFrom), 0);
        int maxx = Math.min(dataSet.getEntryPosition(entryTo) + 1, entries.size());

        int range = (maxx - minx) * 2;
        int to = (int) Math.ceil((maxx - minx) * phaseX + minx);




        SarPointBuffer pointBuffer = mPointBuffers[dataSetIndex];
        pointBuffer.setPhases(phaseX, phaseY);
        pointBuffer.limitFrom(minx);
        pointBuffer.limitTo(maxx);
        pointBuffer.feed(entries);

        trans.pointValuesToPixel(pointBuffer.buffer);

        // draw the body
        for (int j = 0; j < range; j += 2) {

            // get the entry
            SarEntry e = entries.get(j / 2 + minx);

            if (!fitsBounds(e.getXIndex(), mMinX, to))
                continue;


            float pointX = pointBuffer.buffer[j];
            float pointY = pointBuffer.buffer[j + 1];

            if (!Float.isNaN(e.getSar())) {
                if (e.getSar() < e.getClose()) {
                    mRenderPaint.setColor(dataSet.getIncreasingColor());
                } else {
                    mRenderPaint.setColor(dataSet.getDecreasingColor());

                }
                c.drawCircle(pointX, pointY, dataSet.getCircleSize(), mRenderPaint);
            }

        }
    }

    @Override
    public void drawValues(Canvas c) {

    }

    @Override
    public void drawExtras(Canvas c) {

    }

    @Override
    public void drawHighlighted(Canvas c, Highlight[] indices) {

        for (int i = 0; i < indices.length; i++) {

            int xIndex = indices[i].getXIndex(); // get the
            // x-position

            SarDataSet set = mChart.getSarData().getDataSetByIndex(
                    indices[i].getDataSetIndex());

            if (set == null || !set.isHighlightEnabled())
                continue;

            mHighlightPaint.setColor(set.getHighLightColor());

            SarEntry e = set.getEntryForXIndex(xIndex);

            if (e == null || e.getXIndex() != xIndex)
                continue;

//            float low = e.getLow() * mAnimator.getPhaseY();
//            float high = e.getHigh() * mAnimator.getPhaseY();

            float min = mChart.getYChartMin();
            float max = mChart.getYChartMax();

            float[] vertPts = new float[]{
                    xIndex - 0.5f, max, xIndex - 0.5f, min, xIndex + 0.5f, max, xIndex + 0.5f,
                    min
            };


            mChart.getTransformer(set.getAxisDependency()).pointValuesToPixel(vertPts);


            c.drawLine((vertPts[0] + vertPts[4]) / 2, vertPts[1], (vertPts[2] + vertPts[6]) / 2, vertPts[3], mHighlightPaint);

        }
    }
}
